#include <stdio.h>

#define MAX 500

int main()
{
    int n, tmp, len, pre = -1,maxlen = 0;
    scanf("%d", &n);
    int a[10000] = {0};
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &tmp);
        scanf("%d", &len);
        for (int j = 0;; j++)

        {
            scanf("%d", &pre);
            if (pre == 0)
            {
                if (j == 0)
                    a[i] = len;
                break;
            }
            else if (a[i] < a[pre] + len)
            {
                a[i] = a[pre] + len;
            }
        }
        if(a[i] > maxlen)
        maxlen = a[i];
    }
    printf("%d", maxlen);
    return 0;
}